<?php

/**
 * Description of Category
 *
 * @author Kam
 */
class Application_Model_DAO_CatalogCategory extends My_DAO_Abstract {

    const DB_CATALOG_CATEGORY = 'CatalogCategory';

    public function delete($id, $column = 'code') {
        $row = $this->getDbTable()->getByColumn($id, $column);
        if (empty($row)) {
            return false;
        }
        $categoryId = $row['catalog_category_id'];
        $daoCatalog = new Application_Model_DAO_Catalog();
        $rows = $daoCatalog->getByCategoryId($categoryId)->query()->fetchAll();  // pobiera również podkategorie, więc powinno być OK również dla kategori i podkategorii
        $subIds = $this->getSubCategoriesIds($categoryId);
        $subWhere = $this->quoteInto('catalog_category_id IN (?)', array_merge(array(-1), $subIds));
        if (empty($rows)) {
            // jeśli nie ma takich wierszy to można usunąć
            $this->getDbTable()->delete($subWhere);
            $this->getDbTable()->delete($categoryId);
            return 'deleted';
        } else {
            $this->getDbTable()->update(array('is_deleted' => 1), $subWhere);
            $this->getDbTable()->update(array('is_deleted' => 1), $categoryId);
            return 'flagged';
        }
    }

    public function addNew($data) {
        $record = array();
        $this->setSafety($record, $data, 'name');
        $this->setSafety($record, $data, 'icon_path');
        $this->setSafety($record, $data, 'parent_id');
        $record['order_id'] = 1;
        if (!isset($data['parent_id']) || empty($data['parent_id'])) {
            $record['parent_id'] = 0;
        }
        if ($record['parent_id'] > 0) {
            $record['level'] = 2;
        }
        $r = $this->getDbTable()->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                        ->columns(array('m' => new Zend_Db_Expr('MAX(order_id)')))
                        ->where('parent_id = ?', $record['parent_id'])
                        ->query()->fetch();
        $record['order_id'] = empty($r['m']) ? 1 : $r['m'] + 1;
        $recordId = $this->getDbTable()->insert($record);
        $record['category_id'] = $recordId;
        return $record;
    }

    public function getFullCategories() {
        $select = $this->getDbTable()->select()
                ->where('parent_id = 0')
                ->where('level = 1')
                ->where('admin_active = 1')
                ->order(array('order_id', 'catalog_category_id'));
        $result = $select->query()->fetchAll();
        foreach ($result as &$row) {
            $subcategories = $this->getDbTable()->select()
                            ->where('parent_id = ?', $row['catalog_category_id'])
                            ->where('level = 2')
                            ->where('admin_active = 1')
                            ->order(array('order_id', 'catalog_category_id'))
                            ->query()->fetchAll();
            $row['categories'] = $subcategories;
        }
        return $result;
    }

    public function getSubCategoriesIds($parentId) {
        $subCategories = $this->getSubCategories($parentId);
        $result = array();
        if (empty($subCategories)) {
            return $result;
        }
        foreach ($subCategories as $cat) {
            $result[] = $cat['catalog_category_id'];
        }
        return $result;
    }

    public function getSubCategories($parentId) {
        $select = $this->getDbTable()->select()
                ->where('parent_id = ?', $parentId)
                ->where('admin_active = 1')
                ->order(array('order_id', 'catalog_category_id'));
        $cats = $select->query()->fetchAll();
        return $cats;
    }

    protected function getDbTableNameMap() {
        return array(
            self::DB_CATALOG_CATEGORY => 'Application_Model_DbTable_CatalogCategory',
        );
    }

    protected function getDefaultDbTableName() {
        return self::DB_CATALOG_CATEGORY;
    }

}
